// pages/login/index.js
import {loginMpWx}from'../../apis/index'
Page({

  /**
   * 页面的初始数据
   */
  data: {

  },
  goBack(){
    wx.navigateBack({
      delta: 1,
    })
  },
  login() {
    console.log("获取用户信息***")
    const can = wx.canIUse('getUserProfile')
    if (!can) {
      console.log("系统不能使用该函数")
      return
    }
    const _that = this
    wx.getUserProfile({
      desc: '用于快速登录',
      success(result) {
        console.log(result.userInfo)
        _that.onAuthor(result.userInfo)
      },
    })
  },
  async onAuthor(userInfo) {
    console.log("用户信息***", userInfo)
    const code=await this.getCode()
    console.log("获取验code***",code)
    // {"partyData":{"code":"053Uf91w3N17xZ2ltP2w3PLAYh2Uf91E","oauth":"MP-WEIXIN","userInfo":{"nickName":"微信用户","gender":0,"language":"","city":"","province":"","country":"","avatarUrl":"https://thirdwx.qlogo.cn/mmopen/vi_32/POgEwh4mIHO4nibH0KlMECNjjGxQUq24ZEaGT4poC6icRiccVGKSyXwibcPq4BWmiaIGuG1icwxaQX6grC9VemZoJ8rg/132"}}}}
    const partyData = {
      code:code,
      oauth:"MP-WEIXIN",
      userInfo:userInfo
    }
    let form={
      form:{
        partyData: partyData
      }
    }
    console.log("form***",form);
   const logResult=await loginMpWx({
      data:form
    })
    console.log("登录结果为*****",logResult);
    const url = "/pages/register/index?partyData=" + encodeURIComponent(JSON.stringify(partyData)) 
    if (logResult.status === 500){
      wx.navigateTo({
        url: url,
      })
    }
  },
  getCode(){
    return new Promise((resolve,reject) => {
      wx.login({
       success(res){
          resolve(res.code)
       }
      })
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})